package com.qingfeng.base.mpUtil;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.qingfeng.client.bean.Seller;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

import java.util.Date;


/**
 * @Author:zhangyuefei
 * @Date:2019/8/2 17:14
 */

@Component
public class MyBatisObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        try {
        if (metaObject.getValue("createTime")==null) {
            setFieldValByName("createTime", new Date(), metaObject);
            setFieldValByName("updateTime", new Date(), metaObject);
        }else {
            //如果创建时间有，则修改时间和创建时间相同
            setFieldValByName("updateTime", metaObject.getValue("createTime"), metaObject);
        }

            Seller user = (Seller) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            setFieldValByName("createId",user.getId(),metaObject);
            setFieldValByName("updateId",user.getId(),metaObject);
        }catch (Exception e){

        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        try {
            setFieldValByName("updateTime", new Date(), metaObject);
            Seller user = (Seller) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            setFieldValByName("updateId", user.getId(), metaObject);
        }catch (Exception e){

        }
    }
}
